RS485, Modbus & NVT
This category contains functions specific to RS-485 or Modbus devices.
api.rs485Send(msg)
- Overview
- Arguments
- Example
Sends msg to RS485 bus.
Turn on RS485 using rs485State first.
- msg (string) - Data to be sent to RS485 bus
--sends 'test' string to RS485
api.rs485Send('test')
api.rs485Setup(arg1, arg2, ...)
- Overview
- Arguments
- Example
Change the configuration of RS485 interface.
- arg1 - "RX_BUFFER" (string) - set/get size of receive buffer
- arg2 - value (integer)(optional) - set size of the receive buffer
or
- arg1 - baudrate (integer) - Baudrate to use for communication (up to 921600 baud)
- arg2 - parity (integer) - Parity, 0 for none, 1 for odd and 2 for even parity
- arg3 - stopBits (integer) - Number of stop bits, 1 or 2 allowed
- arg4 - dataBits (integer) - Number of data bits, 7 or 8 allowed
--setup RS485 interface to 9600 Baud, 8E1
api.rs485Setup(9600, 2, 1, 8)
api.rs485State(state)
- Overview
- Arguments
- Example
Turns on the RS485 circuitry.
Must be used before rs485Send or rs485Receive.
- state (integer) - New state of RS485 circuitry: 0 for off, 1 for on (fast power-up)
api.rs485State(0) --turn off RS485
api.rs485Receive(timeout,arg1,arg2)
- Overview
- Arguments
- Return
- Example
Waits timeout milliseconds for data reception from RS485 bus.
Turn on RS485 using rs485State() function first.
After the first character is received the inter-character (i.e. inter-byte timeout) delay is 10ms. This can be changed by giving a integer number to arg1.
- timeout (integer) - The maximum time in milliseconds to wait for RS485 device answer
- arg1 (optional) - If string is entered here, it's used as terminator (can be more than one character), otherwise it's inter-byte timeout
- arg2 (optional) - If arg1 is used as inter-byte timeout, this is used to define terminator string (can be more than one character)
- answer (string) - Data received from RS485 bus in given time
- len (integer) - Number of bytes received
--waits 1s for answer from RS485 bus
ans,len = api.rs485Receive(1000)
--waits 1s for answer from RS485 bus and gets everything until it finds "!" character
--stop garbage collector so it doesn't limit the memory (higher speed, prevent's loosing characters at 9600bd)
collectgarbage("stop")
ans,len = api.rs485Receive(1000,"!")
--restart the garbage collector (the memory will overflow, without it)
collectgarbage("restart")
api.modbusCrc(msg)
- Overview
- Arguments
- Return
- Example
Calculates Modbus request checksum.
- msg (string) - Modbus request
- crc (string) - Modbus crc for request
--calculate checksum for Modbus request 110100010002
req = pack.pack('<b6', 0x11,0x01,0x00,0x01,0x00,0x02)
crc = api.modbusCrc(req) --crc = "EE9B"
api.modbusRTU(address, registerAddr, functionCode, typeSpec, registerCount, timeout, retry, interByteTimeout, ignoreCRC)
- Overview
- Arguments
- Return
- Example
The api.modbusRTU() function is used to communicate with a modbus device over a serial connection using the RTU (Remote Terminal Unit) protocol. It can be used to read or write one or more registers of various data types. The function takes several parameters, including the modbus device address, the starting register address, the function code, the type specification of the register(s), the number of registers to read or write, and several timeout and retry parameters.
The typeSpec parameter is a Lua string that specifies the data type of the register(s) being read or written. It uses the Lua Packing and Unpacking syntax to specify the type and formatting of the data. For example, to read a single 16-bit unsigned integer, the typeSpec parameter would be "H".
The function returns the value(s) read from or written to the modbus register(s), according to the specified typeSpec. For example, if typeSpec is "H" and registerCount is 2, the function would return two 16-bit unsigned integers.
If the function fails to communicate with the modbus device, it will retry the operation according to the retry parameter. If the operation still fails after the specified number of retries, the function will return nil.
- address(integer) - The address of the modbus device to communicate with.
- registerAddr(integer) - The starting address of the modbus register to read or write.
- functionCode(integer) - The modbus function code to use for the operation (e.g. read coils, read input registers, write multiple coils, etc.).
- typeSpec(string) - A Lua string that specifies the data type of the register(s) being read or written. More details here.
- if the typeSpec is "", it returns data as received
- registerCount(integer) - The number of registers to read or write.
- timeout(integer) - The timeout (in milliseconds) for the modbus communication operation.
- retry(integer) - The number of times to retry the operation if it fails.
- interByteTimeout(integer) - The timeout (in milliseconds) between receiving bytes during the modbus communication operation.
- ignoreCRC(bool)(optional) - in case this is true, function is not checking modbus CRC.
The function returns the value(s) read from the modbus register(s), according to the specified typeSpec.
--setup the RS485 and turn on the RS485 module
api.rs485Setup(9600,2,1,8) api.rs485State(1)
--read 0x4003 register (modbus ID in case of INEPRO PRO1)
x=api.modbusRTU(10,0x4003,3,">h",1,2000,3,10) print(x)
api.nvtProcess(buf)
- Overview
- Arguments
- Return
- Example
Processes NVT message and either sets baudrate, datasize, parity or stop size for MBUS or MODBUS.
- buf (string) - Message to be processed
- msg (string) - Message without NVT sequence
- answer (string) - NVT answer
-- If massage received (buf) send it to RS485
ret,port,buf = api.loraSend(0,1000,data)
if buf ~= nil then
buf, nvtans = api.nvtProcess(buf)
api.rs485Send(buf)
end
api.nvtEncode(msg)
- Overview
- Arguments
- Return
- Example
Encodes message to NVT format
- msg (string) - Message to be encoded to NVT
- answer (string) - NVT message
ans,len = api.rs485Receive(50)
ans = api.nvtEncode(ans)
api.loraSend(0,1,ans)